package com.chixing.service.impl;

import com.chixing.mapper.CustomerMapper;
import com.chixing.mapper.SightsCollectMapper;
import com.chixing.mapper.TravelSightsMapper;
import com.chixing.pojo.SightsCollect;
import com.chixing.pojo.SightsCollectExample;
import com.chixing.pojo.TravelSights;
import com.chixing.pojo.TravelSightsExample;
import com.chixing.service.SightsCollectionService;
import com.chixing.util.ServerResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SightsCollectionServiceImpl  implements SightsCollectionService {

    @Autowired
    private SightsCollectMapper sightsCollectMapper;
    @Autowired
    private TravelSightsMapper travelSightsMapper;
    @Override
    public ServerResponse getSightsbycustId(Integer custId) {
        SightsCollectExample sightsCollectExample=new SightsCollectExample();
        sightsCollectExample.createCriteria().andCustIdEqualTo(custId);
        List<SightsCollect> sightsCollects=sightsCollectMapper.selectByExample(sightsCollectExample);
        List<TravelSights> travelSightsArrayList=new ArrayList<>();
        for(SightsCollect sightsCollect :sightsCollects){
            TravelSights travelSights=travelSightsMapper.selectByPrimaryKey(sightsCollect.getTravelSightsId());
            travelSightsArrayList.add(travelSights);
        }
        return ServerResponse.getSuccess(travelSightsArrayList);
    }

    @Override
    public ServerResponse getByCustIdAndPage(Integer custId, Integer pageNum) {
        SightsCollectExample sightsCollectExample=new SightsCollectExample();
        sightsCollectExample.createCriteria().andCustIdEqualTo(custId);
        List<SightsCollect> sightsCollects=sightsCollectMapper.selectByExample(sightsCollectExample);
        List<TravelSights> travelSightsArrayList=new ArrayList<>();
        PageHelper.startPage(pageNum,5);

        for(SightsCollect sightsCollect :sightsCollects){

            TravelSights travelSights=travelSightsMapper.selectByPrimaryKey(sightsCollect.getTravelSightsId());
            travelSightsArrayList.add(travelSights);
        }
        PageInfo<TravelSights> pageInfo = new PageInfo<>(travelSightsArrayList);
        if(travelSightsArrayList!=null && travelSightsArrayList.size() >0){
            return ServerResponse.getSuccess(pageInfo);
        }
        return ServerResponse.getFail(pageInfo);

    }


}
